package com.demo.web.pc;

import com.demo.common.Constants;
import com.demo.dao.ApplySendMapper;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.*;
import com.demo.entity.ApplySend;
import com.demo.service.ISalePersonAccountService;
import com.demo.service.ISalePersonService;
import com.demo.service.ISpFavorService;
import com.demo.vo.SalePersonVo;

import com.demo.entity.SalePerson;
import com.demo.entity.SalePersonAccount;
import com.demo.entity.SpFavor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/saleperson")
public class SalePersonPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISalePersonService depService;
    @Autowired
    private ISalePersonAccountService spaService;
    @Autowired
    private ISpFavorService sfService;
    @Autowired
    private ApplySendMapper asDao;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SalePerson> get(SalePerson arg) {
        SalePerson service_res = depService.selectByPrimaryKey(arg.getId());
        BaseResult<SalePerson> res = new BaseResult<SalePerson>(true, service_res);
        return res;
    }
//

    /**
     * login
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult<SalePerson> login(SalePerson arg, HttpSession session) {
        SalePerson service_res = depService.selectByPhone(arg);
        BaseResult<SalePerson> res;
        if (service_res != null) {
            service_res.setPassword(null);
            session.setAttribute("current", service_res);
            res = new BaseResult<SalePerson>(true, service_res);
        } else {
            res = new BaseResult(false, "用户不存在", 503);
        }
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public BaseResult<Object> logout(SalePerson arg, HttpSession session) {
        BaseResult<Object> res;
        session.setAttribute("current", null);
        res = new BaseResult(true, "success");
        return res;
    }

    /**
     * 修改头像
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update-thumb", method = RequestMethod.POST)
    public BaseResult<Integer> updateThumb(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        BaseResult<Integer> res;
        if (sale != null) {
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
            session.setAttribute("current", sp);
        } else {
            res = new BaseResult<Integer>(false, "请登录!", 401);
        }
        return res;
    }

    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        BaseResult<Integer> res;
        if (sale != null) {
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
            session.setAttribute("current", sp);

        } else {
            res = new BaseResult<Integer>(false, "请登录!", 401);
        }
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public BaseResult<SalePerson> current(SalePerson arg, HttpSession session) {
//        return new BaseResult(false, AdvancedUtil.wxAppId, 401); // for test
        SalePerson sale = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        BaseResult<SalePerson> res;
        if (sale != null) {
            res = new BaseResult<SalePerson>(true, sale);
        } else {
            res = new BaseResult(false, "请登录!", 401);

        }
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/currentCashAccount", method = RequestMethod.GET)
    public BaseResult<Object> currentCashAccount(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        BaseResult<Object> res;
        if (sale != null) {
            SalePersonAccount spa = spaService.selectBySaleId(String.valueOf(sale.getId()));
//            SalePerson sp = new SalePerson();
//            sp.setValid();
            Map map = new HashMap();
            map.put("account", spa);
            map.put("person", sale);
//            sp.setValid(sale.getValid());
            res = new BaseResult(true, map);
        } else {
            res = new BaseResult(false, "请登录!", 401);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SalePerson> query(SalePersonVo arg) {
        List<SalePerson> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }


    @ResponseBody
    @RequestMapping(value = "/favor", method = RequestMethod.GET)
    public BaseResult<Object> query(SpFavor arg, String status, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new BaseResult(false, "请登录!", 401);
        }
        arg.setUserId(String.valueOf(sp.getId()));
        if (arg.getOfferId() == null) {
            return new BaseResult(false, "参数错误!", 403);
        }
        if ("1".equals(status)) {
            int res = sfService.insertSelective(arg);
            if (res == 1) {
                return new BaseResult(true, "success");
            } else {
                return new BaseResult(false, "关注失败!", 500);
            }
        } else if ("0".equals(status)) {
            int res = sfService.deleteByUserIdAndOfferId(arg);
            if (res == 1) {
                return new BaseResult(true, "success");
            } else {
                return new BaseResult(false, "关注失败!", 500);
            }
        } else {
            return new BaseResult(false, "无权操作!", 403);
        }
    }

    //    queryByUserId
    @ResponseBody
    @RequestMapping(value = "/mySales", method = RequestMethod.GET)
    public ListResult<SalePerson> queryMySales(SalePersonVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new ListResult<SalePerson>(false, "未登录", 401);
        }
        arg.setUserId(String.valueOf(sp.getId()));
        List<SalePerson> list = depService.queryByUserIdAndPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    //
    @ResponseBody
    @RequestMapping(value = "/queryFocus", method = RequestMethod.GET)
    public ListResult queryFocus(HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new ListResult(false, "请登录!", 401);
        }
        List<SpFavor> list = sfService.queryByUserId(String.valueOf(sp.getId()));
        ListResult<SalePerson> res = new ListResult(true, list);
        return res;
    }


    /**
     * 申请发货操作人授权书
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public BaseResult apply(ApplySend arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new BaseResult(false, "请登录!", 401);
        }
        ApplySend as = asDao.selectBySaleId(String.valueOf(sp.getId()));
        BaseResult res = null;
        if (as != null) {
            as.setUrl(arg.getUrl());
            as.setStatus("0");
            as.setClienteleId(arg.getClienteleId());
            java.util.Date ud = new java.util.Date();
            as.setCreateAt(new Date(ud.getTime()));
            int res_insert = asDao.updateByPrimaryKey(as);
            if (res_insert == 1) {
                res = new BaseResult(true, "success");
            } else {
                res = new BaseResult(false, "操作失败!", 500);
            }
        } else {
            arg.setSaleId(String.valueOf(sp.getId()));
            int res_insert = asDao.insertSelective(arg);
            if (res_insert == 1) {
                res = new BaseResult(true, "success");
            } else {
                res = new BaseResult(false, "操作失败!", 500);
            }
        }
        return res;
    }

    /**
     * 判断是否已申请发货操作人
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/judge-apply", method = RequestMethod.POST)
    public BaseResult apply(HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new BaseResult(false, "请登录!", 401);
        }
        ApplySend as = asDao.selectBySaleId(String.valueOf(sp.getId()));
        BaseResult res = null;
        if (as != null) {
            res = new BaseResult(true, as);
        } else {
            res = new BaseResult(false, "操作失败!", 500);
        }
        return res;
    }

}
